package com.ruoyi.model.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.domain.ModelPlanAlgorithm;
import com.ruoyi.model.domain.ModelTaskProcess;
import com.ruoyi.model.domain.ModelTaskProcessParam;
import com.ruoyi.model.domain.vo.ModelPlanAlgorithmVo;
import com.ruoyi.model.domain.vo.ModelTaskProcessParamVo;
import com.ruoyi.model.domain.vo.ModelTaskProcessVo;
import com.ruoyi.model.domain.vo.ModelTaskVo;
import com.ruoyi.model.service.IModelTaskProcessParamService;
import com.ruoyi.model.service.IModelTaskProcessService;
import com.ruoyi.model.service.MqttGateway;
import com.ruoyi.model.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.ModelTaskMapper;
import com.ruoyi.model.domain.ModelTask;
import com.ruoyi.model.service.IModelTaskService;

/**
 * 模型任务Service业务层处理
 *
 * @author rww
 * @date 2024-11-25
 */
@Service
public class ModelTaskServiceImpl implements IModelTaskService
{
    @Autowired
    private ModelTaskMapper modelTaskMapper;

    @Autowired
    private MqttGateway mqttGateway;
    @Value("${spring.mqtt.service}")
    private String mqttservice;

    @Autowired
    private IModelTaskProcessParamService modelTaskProcessParamService;

    @Autowired
    private IModelTaskProcessService modelTaskProcessService;

    /**
     * 查询模型任务
     *
     * @param id 模型任务主键
     * @return 模型任务
     */
    @Override
    public ModelTask selectModelTaskById(Long id)
    {
        return modelTaskMapper.selectModelTaskById(id);
    }

    @Override
    public ModelTaskVo selectModelTaskVoById(Long id) {
        ModelTask info= selectModelTaskById(id);
        ModelTaskVo taskVo=BeanUtil.copyProperties(info,ModelTaskVo.class);
        if(ObjectUtil.isNotEmpty(taskVo)) {
            ModelTaskProcess taskProcess = new ModelTaskProcess();
            taskProcess.setTaskId(taskVo.getId());
            List<ModelTaskProcessVo> taskProcessList = modelTaskProcessService.selectModelTaskProcessVoList(taskProcess);
            if (ObjectUtil.isNotEmpty(taskProcessList)) {
                for (ModelTaskProcessVo process : taskProcessList) {
                    ModelTaskProcessParam param = new ModelTaskProcessParam();
                    param.setAlgorithmId(process.getAlgorithmId());
                    param.setParamType("2");
                    param.setTaskId(id);
                    List<ModelTaskProcessParamVo> paramList = modelTaskProcessParamService.selectModelTaskProcessParamList(param);
                    process.setParamList(paramList);
                }
                taskVo.setProcessList(taskProcessList);
            }
        }
        return taskVo;
    }

    /**
     * 查询模型任务列表
     *
     * @param modelTask 模型任务
     * @return 模型任务
     */
    @Override
    public List<ModelTask> selectModelTaskList(ModelTask modelTask)
    {
        return modelTaskMapper.selectModelTaskList(modelTask);
    }
    /**
     * 新增模型任务
     *
     * @param modelTask 模型任务
     * @return 结果
     */
    @Override
    public int insertModelTask(ModelTask modelTask)
    {
        modelTask.setId(IdUtil.getSnowflakeNextId());
        modelTask.setCreateTime(DateUtils.getNowDate());
        return modelTaskMapper.insertModelTask(modelTask);
    }

    @Override
    public int insertModelTaskVo(ModelTaskVo modelTask)
    {

        int num=insertModelTask(modelTask);
        if(num>0) {
            for (ModelTaskProcessVo taskProcessVo : modelTask.getProcessList()) {
                taskProcessVo.setTaskId(modelTask.getId());
                modelTaskProcessService.insertModelTaskProcess(taskProcessVo);
                for (ModelTaskProcessParamVo paramVo:taskProcessVo.getParamList()){
                    paramVo.setParamType("2");
                    paramVo.setTaskId(modelTask.getId());
                    modelTaskProcessParamService.insertModelTaskProcessParam(paramVo);
                }
            }
        }

        return num;
    }

    @Override
    public int updateModelTaskVo(ModelTaskVo modelTask) {
        int num=updateModelTask(modelTask);
        if(num>0){

            deleteProcess(modelTask.getId());

            for (ModelTaskProcessVo taskProcessVo : modelTask.getProcessList()) {
                taskProcessVo.setTaskId(modelTask.getId());
                modelTaskProcessService.insertModelTaskProcess(taskProcessVo);
                for (ModelTaskProcessParamVo paramVo:taskProcessVo.getParamList()){
                    paramVo.setParamType("2");
                    paramVo.setTaskId(modelTask.getId());
                    modelTaskProcessParamService.insertModelTaskProcessParam(paramVo);
                }
            }
        }
        return num;
    }

    private void deleteProcess(Long id){
        ModelTaskProcess taskProcess=new ModelTaskProcess();
        taskProcess.setTaskId(id);
        List<ModelTaskProcess> taskProcessList=modelTaskProcessService.selectModelTaskProcessList(taskProcess);
        if(ObjectUtil.isNotEmpty(taskProcessList)) {
            for (ModelTaskProcess process : taskProcessList) {
                ModelTaskProcessParam param = new ModelTaskProcessParam();
                param.setAlgorithmId(process.getAlgorithmId());
                param.setParamType("2");
                param.setTaskId(id);
                List<ModelTaskProcessParamVo> paramList = modelTaskProcessParamService.selectModelTaskProcessParamList(param);
                if (ObjectUtil.isNotEmpty(paramList)) {
                    Long[] ids= paramList.stream().map(ModelTaskProcessParam::getId).toArray((Long[]::new));
                    modelTaskProcessParamService.deleteModelTaskProcessParamByIds(ids);
                }
                modelTaskProcessService.deleteModelTaskProcessById(process.getId());
            }
        }
    }

    /**
     * 修改模型任务
     *
     * @param modelTask 模型任务
     * @return 结果
     */
    @Override
    public int updateModelTask(ModelTask modelTask)
    {
        modelTask.setUpdateTime(DateUtils.getNowDate());
        return modelTaskMapper.updateModelTask(modelTask);
    }

    /**
     * 批量删除模型任务
     *
     * @param ids 需要删除的模型任务主键
     * @return 结果
     */
    @Override
    public int deleteModelTaskByIds(Long[] ids)
    {
        return modelTaskMapper.deleteModelTaskByIds(ids);
    }

    /**
     * 删除模型任务信息
     *
     * @param id 模型任务主键
     * @return 结果
     */
    @Override
    public int deleteModelTaskById(Long id)
    {
        deleteProcess(id);

        return modelTaskMapper.deleteModelTaskById(id);
    }

    @Override
    public int execModelTaskVoById(Long id) {
        ModelTask info= selectModelTaskById(id);
        ModelTaskVo taskVo=BeanUtil.copyProperties(info,ModelTaskVo.class);
        if(ObjectUtil.isNotEmpty(taskVo)) {
            taskVo.setTaskStatus("1");
            taskVo.setStartTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
            updateModelTask(taskVo);

            ModelTaskProcess taskProcess = new ModelTaskProcess();
            taskProcess.setTaskId(taskVo.getId());
            List<ModelTaskProcessVo> taskProcessVos = modelTaskProcessService.selectModelTaskProcessVoList(taskProcess);
            if (ObjectUtil.isNotEmpty(taskProcessVos)) {
                for (ModelTaskProcessVo process : taskProcessVos) {
                    ModelTaskProcessParam param = new ModelTaskProcessParam();
                    param.setAlgorithmId(process.getAlgorithmId());
                    param.setParamType("2");
                    param.setTaskId(id);
                    List<ModelTaskProcessParamVo> paramList = modelTaskProcessParamService.selectModelTaskProcessParamList(param);

                    process.setParamList(paramList);

                    mqttGateway.sendToMqtt(JSONUtil.toJsonStr(process),mqttservice+"/model-task/exec");

//                    process.setProcessStatus("1");
//                    modelTaskProcessService.updateModelTaskProcess(process);
                }
            }
        }
        return 1;
    }
}
